# -*- coding: utf8 -*-

import os
import webapp2

from google.appengine.api import taskqueue
from google.appengine.api.taskqueue import Queue

from statistics.word_statistic import *
from game_history import *

import constants


class DeleteWordStatistic(webapp2.RequestHandler):
    def post(self):
        ndb.Key(urlsafe=self.request.get("key")).delete()


class AddAllStatistic(webapp2.RequestHandler):
    def post(self):
        for key_word_stat in WordStatistic.query().iter(keys_only=True):
            taskqueue.add(url='/delete_word_statistic',
                          queue_name=constants.WORD_STATISTIC_QUEUE,
                          params={'key': key_word_stat.urlsafe()})
        for key_hist in GameHistory.query().iter(keys_only=True):
            taskqueue.add(url='/add_word_statistic_from_game',
                          queue_name=constants.WORD_STATISTIC_QUEUE,
                          params={'key': key_hist.urlsafe()})


class RecalcAllStatisticFromQueue(webapp2.RequestHandler):
    def post(self):
        Queue(constants.WORD_STATISTIC_QUEUE).purge()
        taskqueue.add(url='/add_all_stat', countdown=20)


class RecalcAllStatistic(webapp2.RequestHandler):
    def get(self):
        taskqueue.add(url='/recalc_all_stat_from_queue',
                      queue_name=constants.WORD_STATISTIC_QUEUE,
                      params={})
